package Logic;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.AppointmentDB;
import Database.AuditLogDB;
import Model.Committee;
import Model.CommitteeRole;

/**
 * Servlet implementation class CommitteeNew_BLL
 */

public class CommitteeNew_BLL extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommitteeNew_BLL() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String title = request.getParameter("title");
		String roleResponsibility = request.getParameter("r&r");
		String purpose = request.getParameter("purpose");
		String frequencyofmeeting = request.getParameter("fom");
		String date = request.getParameter("effectivedate");
		
		if(title != null && title != "")
		{
		request.setAttribute("title",title);
		}
		if(roleResponsibility != null && roleResponsibility != "")
		{
		request.setAttribute("r&r",roleResponsibility);
		}
		if(purpose != null && purpose != "")
		{
		request.setAttribute("purpose",purpose);
		}
		if(frequencyofmeeting != null && frequencyofmeeting != "")
		{
		request.setAttribute("fom",frequencyofmeeting);
		}
		if(date != null && date != "")
		{
		request.setAttribute("effectivedate", date);
		}

		if (title == "" | roleResponsibility == "" | purpose == "" | date == "") {
			
			request.getRequestDispatcher(
					"committee_newcomm.jsp?action=e&error=1")
					.forward(request, response);
		} else if (Database.CommitteeDB.titleExists(title)) {
			request.getRequestDispatcher(
					"committee_newcomm.jsp?action=e&error=2")
					.forward(request, response);
		} else {
			Committee com = new Committee();
			
			//problem here
			String committeeId = Integer.toString(Database.CommitteeDB
					.getLastNumber() + 1);
			com.setId(committeeId);
			com.setTitle(title);
			com.setPurpose(purpose);
			com.setRolesResponsibility(roleResponsibility);
			com.setFrequencyOfMeeting(frequencyofmeeting);
			com.setDate(date);
			com.setCreatedBy(
					(String) request.getSession().getAttribute("username"));

			Database.CommitteeDB.createCommittee(com);
			AuditLogDB.insertLog("create committee", (String) request
					.getSession().getAttribute("name"));

			request.setAttribute("title",title);
			request.setAttribute("committeeId",committeeId);
			
			ArrayList<CommitteeRole> commList = Database.CommitteeRoleDB
					.getCommitteeRole(committeeId);
			int listSize = commList.size();
			request.setAttribute("commListRow", listSize);
			request.getRequestDispatcher(
					"committee_newcomm2.jsp?action=e&id=" + committeeId +
					"&error=-2")
					.forward(request, response);
			
			
//			response.sendRedirect("committee_newcomm2.jsp?error=-1");
		}

	}
	

}
